package com.wechat.wechat.controller;

/**
 * @author NN
 * @2020/2/20 10:51
 */

import com.wechat.wechat.entity.ProductCategory;
import com.wechat.wechat.entity.ProductInfo;
import com.wechat.wechat.service.CategoryService;
import com.wechat.wechat.service.ProductService;
import com.wechat.wechat.utils.ResultVOUtil;
import com.wechat.wechat.vo.ProductInfoVO;
import com.wechat.wechat.vo.ProductVO;
import com.wechat.wechat.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    /**
     *Cacheable将数据加到redis缓存中 127.0.0.1:8080/sell/buyer/product/list
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "product" ,key="productlist")
    public ResultVO list()
    {
        //1.查询所有上架商品
        List<ProductInfo> productInfoList=productService.findAllUp();

        //2.查询所有商品类别
        /*
        List<Integer> types=new ArrayList<Integer>();
        for(ProductInfo productInfo:productInfoList){
            types.add(productInfo.getCategoryType());
        }

        */
        /*利用java8 lambal */
        List<Integer> types=productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList=
                categoryService.findByCategoryTypeIn(types);
        //3.数据拼装 类别->商品list
        ResultVO result=new ResultVO();
        List<ProductVO> productVOList=new ArrayList<ProductVO>();
        for(ProductCategory productCategory:productCategoryList){
            ProductVO productVO=new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVO> ProductInfoVOList=new ArrayList<ProductInfoVO>();
            for(ProductInfo productInfo:productInfoList)
            {
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType()))
                {
                    ProductInfoVO productInfoVO=new ProductInfoVO();
                    /*将productInfo字段值拷贝给productInfoVO*/
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    ProductInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(ProductInfoVOList);
            productVOList.add(productVO);
        }
        return ResultVOUtil.success(productVOList);
    }
}
